package com.kaifamiao.wendao.servlet;

import com.kaifamiao.wendao.entity.Topic;
import com.kaifamiao.wendao.service.TopicService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet( "/topic/detail" )
public class TopicDetailServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request , HttpServletResponse response )
            throws ServletException, IOException {
        // 从查询字符串中获取请求参数
        String idString = request.getParameter( "id" );
        // 解析字符串为整数并返回其相应的包装类类型的实例
        Integer id = Integer.valueOf( idString );

        TopicService ts = new TopicService();
        // 查询指定id对应的话题
        Topic t = ts.load( id );

        // 将 话题 设置到 请求对象 的属性中
        request.setAttribute( "topic" , t );

        String path = "/WEB-INF/pages/topic/detail.jsp" ;
        // 去往详情页面
        RequestDispatcher dispatcher = request.getRequestDispatcher( path );
        dispatcher.forward( request , response );
    }

}
